package com.bawei.module_mine.applyfor

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.RequestEvent
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.base.utils.LoggerUtils
import com.bawei.lib_net.storage.AuthMMKV
import com.bawei.module_mine.model.CarOrderModelItem
import com.bawei.module_mine.model.InspectionOrderModelItem
import com.bawei.module_mine.model.MineServiceModel
import com.bawei.module_mine.model.RepairOrderModelItem
import com.bawei.module_mine.model.VisitOrderModelItem
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.zip
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class ApplyForViewModel @Inject constructor(
    private val mRepository: ApplyForRepository
) : BaseViewModel(){
    //车位申请
    private val _parkingEvent = MutableSharedFlow<RequestEvent>()
    val parkingEvent get() = _parkingEvent.asSharedFlow()
    var parkingList: MutableList<CarOrderModelItem> = mutableListOf()
    private val _parkingList = MutableLiveData<List<CarOrderModelItem>>()
    val parkingList1: LiveData<List<CarOrderModelItem>> get() = _parkingList

    //访客申请
    private val _visitEvent = MutableSharedFlow<RequestEvent>()
    val visitEvent get() = _visitEvent.asSharedFlow()
    var visitList: MutableList<VisitOrderModelItem> = mutableListOf()

    //维修申请
    private val _repairEvent = MutableSharedFlow<RequestEvent>()
    val repairEvent get() = _repairEvent.asSharedFlow()
    var repairList: MutableList<RepairOrderModelItem> = mutableListOf()

    //巡更申请
    private val _inspectionEvent = MutableSharedFlow<RequestEvent>()
    val inspectionEvent get() = _inspectionEvent.asSharedFlow()
    var inspectionList: MutableList<InspectionOrderModelItem> = mutableListOf()
//    fun initApplyFor(){
//        viewModelScope.launch {
//            mRepository.parkingApply()
//                .zip(mRepository.visitApply()){
//                    parking,visit ->
//                    visitList = visit.data as MutableList<VisitOrderModelItem>
//                    LoggerUtils.d("ApplyForViewModel1",visitList.toString())
//                    parking
//                }
//                .zip(mRepository.repairApply()){
//                    parking,repair ->
//                    repairList = repair.data as MutableList<RepairOrderModelItem>
//                    LoggerUtils.d("ApplyForViewModel2",repairList.toString())
//                    parking
//                }
//                .zip(mRepository.inspectionApply()){
//                    parking,inspection ->
//                    inspectionList = inspection.data as MutableList<InspectionOrderModelItem>
//                    LoggerUtils.d("ApplyForViewModel3",inspectionList.toString())
//                    parking
//                }.handleApiException2Model()
//                .collect{
//                    parkingList = it.data as MutableList<CarOrderModelItem>
//                    LoggerUtils.d("ApplyForViewModel4",parkingList.toString())
////                    _parkingEvent.emit(Success)
////                    _visitEvent.emit(Success)
////                    _repairEvent.emit(Success)
////                    _inspectionEvent.emit(Success)
//                }
//        }
//    }
    fun getsParkingList(){
        execute(
            mRepository.parkingApply(),
            onStart = {
                _parkingEvent.emit(Loading)
            },
            onError = {
                _parkingEvent.emit(Failed(it))
            },
            onFinish = {
                parkingList.clear()
                parkingList.addAll(it.data)
                _parkingEvent.emit(Success)
            }
        )
    }
    fun getVisitList(){
        execute(
            mRepository.visitApply(),
            onStart = {
                _visitEvent.emit(Loading)
            },
            onError = {
                _visitEvent.emit(Failed(it))
            },
            onFinish = {
                visitList.clear()
                visitList.addAll(it.data)
                _visitEvent.emit(Success)

            }
        )
    }

    fun getRepairList(){
        execute(
            mRepository.repairApply(),
            onStart = {
                _repairEvent.emit(Loading)
            },
            onError = {
                _repairEvent.emit(Failed(it))
            },
            onFinish = {
                repairList.clear()
                repairList.addAll(it.data)
                _repairEvent.emit(Success)
            }
        )
    }
    fun getInspectionList() {
        execute(
            mRepository.inspectionApply(),
            onStart = {
                _inspectionEvent.emit(Loading)
            },
            onError = {
                _inspectionEvent.emit(Failed(it))
            },
            onFinish = {
                inspectionList.clear()
                inspectionList.addAll(it.data)
                _inspectionEvent.emit(Success)
            }
        )
    }


}